/**
 * 
 */
package cream.alarm.activity;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import cream.alarm.R;
import cream.alarm.db.DbAdapter;
import cream.alarm.helper.WeekdayHelper;
import cream.alarm.helper.impl.WeekdayHelperImpl;
import cream.alarm.model.Weekday;

/**
 * 工作日设置页面
 * 
 * @author Cream
 * @since 1.0 2012-01-23
 */
public class WeekdayList extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.weekday_list);

		WeekdayHelper wh = new WeekdayHelperImpl(this);
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 7);
		Date week = cal.getTime();

		List<Weekday> list = wh.listWeekday(now, week);

		// Filling weekday list
		ListAdapter adapter = new SimpleAdapter(this, wh.asSimpleAdapterData(
				this, list), R.layout.weekday_list_row, new String[] {
				DbAdapter.KEY_DATE, DbAdapter.KEY_IS_WEEKDAY,
				DbAdapter.KEY_DESC }, new int[] { R.id.weekday_list_date,
				R.id.weekday_list_is_weekday, R.id.weekday_list_desc });
		setListAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.NONE, Menu.FIRST,
				R.string.option_menu_set_weekday);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getOrder() == Menu.FIRST) {
			Intent i = new Intent();
			i.setClass(WeekdayList.this, AddWeekday.class);
			startActivityForResult(i, 0);
		}
		return super.onOptionsItemSelected(item);
	}

}
